package mi.payment.settings;

import mi.payment.utils.SharedPreferencesUtil;

import static mi.payment.settings.NetworkParamSettings.NETWORK_PARAMS_KEY;

/**
 * 网络参数设置数据操作
 */
public class NetworkDataOperations {

    private static NetworkDataOperations instance;
    private NetworkParamSettings instanceSettings;

    private NetworkDataOperations() {
        instanceSettings = getNetworkParamSettings();
        if (instanceSettings == null) {
            instanceSettings = new NetworkParamSettings();
            saveData(instanceSettings);
        }
    }

    public static NetworkDataOperations getInstance() {
        if (instance == null) {
            instance = new NetworkDataOperations();
        }
        return instance;
    }

    public NetworkParamSettings getNetworkParamSettings() {
        if (instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(NETWORK_PARAMS_KEY);
            if (obj instanceof NetworkParamSettings) {
                instanceSettings = (NetworkParamSettings) obj;
            } else {
                instanceSettings = new NetworkParamSettings();
                saveData(instanceSettings);
            }
        }
        return instanceSettings;
    }

    public void init(String tPdu, String hostIpAddress, String hostPort,
                     String communicationTimeout, Boolean automaticGetIp,
                     String spareHostIpAddress, String spareHostPort,
                     int transactionRepeatNum, int connectionMode,
                     int networkDefaultConfiguration,
                     boolean networkInterventionFunction, boolean dns) {
        NetworkParamSettings bean = getNetworkParamSettings();
        bean.setTPdu(tPdu);
        bean.setHostIpAddress(hostIpAddress);
        bean.setHostPort(hostPort);
        bean.setCommunicationTimeout(communicationTimeout);
        bean.setAutomaticGetIp(automaticGetIp);
        bean.setSpareHostIpAddress(spareHostIpAddress);
        bean.setSpareHostPort(spareHostPort);
        bean.setTransactionRepeatNum(transactionRepeatNum);
        bean.setConnectionMode(connectionMode);
        bean.setNetworkDefaultConfiguration(networkDefaultConfiguration);
        bean.setNetworkInterventionFunction(networkInterventionFunction);
        bean.setDns(dns);
        saveData(bean);
    }

    public void saveTPDU(String tPdu) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setTPdu(tPdu);
        saveData(network);
    }

    /**
     * 设置主机IP地址
     *
     * @param hostIpAddress 要设置的主机IP地址
     */
    public void saveHostIpAddress(String hostIpAddress) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setHostIpAddress(hostIpAddress);
        saveData(network);
    }

    /**
     * 设置主机端口
     *
     * @param hostPort 要设置的主机端口
     */
    public void saveHostPort(String hostPort) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setHostPort(hostPort);
        saveData(network);
    }

    /**
     * 设置通讯超时时间
     *
     * @param communicationTimeout 要设置的通讯超时时间
     */
    public void saveCommunicationTimeout(String communicationTimeout) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setCommunicationTimeout(communicationTimeout);
        saveData(network);
    }

    /**
     * 是否自动获取IP
     *
     * @param automaticGetIp true 是/false 否
     */
    public void saveAutomaticGetIp(boolean automaticGetIp) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setAutomaticGetIp(automaticGetIp);
        saveData(network);
    }

    /**
     * 备份服务器IP
     */
    public void saveSpareHostIpAddress(String spareHostIpAddress) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setSpareHostIpAddress(spareHostIpAddress);
        saveData(network);
    }

    /**
     * 备份服务器端口
     */
    public void saveSpareHostPort(String spareHostPort) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setSpareHostPort(spareHostPort);
        saveData(network);
    }

    public void saveThirdServerIpAddress(String thirdServerIpAddress) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setThirdServerIpAddress(thirdServerIpAddress);
        saveData(network);
    }

    public void saveThirdServerPort(String thirdServerPort) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setThirdServerPort(thirdServerPort);
        saveData(network);
    }

    public void saveThirdServerIpAddressBackup(String thirdServerIpAddressBackup) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setThirdServerIpAddressBackup(thirdServerIpAddressBackup);
        saveData(network);
    }

    public void saveThirdServerPortBackup(String thirdServerPortBackup) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setThirdServerPortBackup(thirdServerPortBackup);
        saveData(network);
    }

    /**
     * 交易重播次数
     */
    public void saveTransactionRepeatNum(int transactionRepeatNum) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setTransactionRepeatNum(transactionRepeatNum);
        saveData(network);
    }

    /**
     * 连接方式	0短连接／1长连接
     */
    public void saveConnectionMode(int connectionMode) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setConnectionMode(connectionMode);
        saveData(network);
    }

    /**
     * 默认网络配置	配置1  ／配置2
     */
    public void saveNetworkDefaultConfiguration(int networkDefaultConfiguration) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setNetworkDefaultConfiguration(networkDefaultConfiguration);
        saveData(network);
    }

    /**
     * 获取备份服务器IP地址
     */
    public void saveNetworkInterventionFunction(boolean networkInterventionFunction) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setNetworkInterventionFunction(networkInterventionFunction);
        saveData(network);
    }

    /**
     * 保存dns
     */
    public void saveDns(boolean dns) {
        NetworkParamSettings network = getNetworkParamSettings();
        network.setDns(dns);
        saveData(network);
    }

    public void saveData(NetworkParamSettings networkParamSettings) {
        this.instanceSettings = networkParamSettings;
        SharedPreferencesUtil.saveObj(networkParamSettings, NETWORK_PARAMS_KEY);
    }

    public void clearNetworkParamSettings() {
        SharedPreferencesUtil.clearByKey(NETWORK_PARAMS_KEY);
    }

}
